﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Infrastructure.Account;
using Gfw.Repositories.Account;
using Gfw.Repository;

namespace Gfw.Infrastructure.Facilities
{
    /// <summary>
    /// 分红奖，适用所有的动态奖金分红，给其他人
    /// </summary>
    public class Dividends : IFacility
    {
        private decimal _amount;
        private string _toUserId;

        /// <summary>
        /// 直推将，将从公司帐户（奖金预提）转入用户帐户
        /// </summary>
        /// <param name="userAmount">直接推奖获奖用户帐号</param>
        /// <param name="amount">奖金金额</param>
        public Dividends(string toUserId, decimal amount)
        {
            this._toUserId = toUserId;
            this._amount = amount;
        }


        public void Execute(Repository.UnitOfWork unitOfWork = null)
        {

            UnitOfWork work = unitOfWork ?? new UnitOfWork();

            string awardSerialNo = Guid.NewGuid().ToString();

            //用户上税
            AwardTax at = new AwardTax(_toUserId, _amount, awardSerialNo, null);
            at.Execute(work);

            //奖金帐户分配
            AwardAllot aa = new AwardAllot(_toUserId, at.IndividualAward(), awardSerialNo, null,
                                           UserTradeTypeEnum.Dividends);
            aa.Execute(work);

            //个人用户的奖金
            decimal individual = at.IndividualAward();
            decimal tax = at.Tax();
            //插入动态奖金分配表
            ActUserDynamicawardRepository userAwardRepository = new ActUserDynamicawardRepository();
            userAwardRepository.AddAward(_toUserId, (int)UserTradeTypeEnum.Dividends, _amount, individual, tax,
                                         awardSerialNo, work);
            if (unitOfWork == null)
                work.Commit();
        }
    }
}
